(function () {
  'use strict';

  /**
   * Targets special code or div blocks and converts them to UML.
   * @param {object} converter is the object that transforms the text to UML.
   * @param {string} className is the name of the class to target.
   * @param {object} settings is the settings for converter.
   * @return {void}
   */
  var uml = (function (converter, className, settings) {

    var getFromCode = function getFromCode(parent) {
      // Handles <pre><code>
      var text = "";
      for (var j = 0; j < parent.childNodes.length; j++) {
        var subEl = parent.childNodes[j];
        if (subEl.tagName.toLowerCase() === "code") {
          for (var k = 0; k < subEl.childNodes.length; k++) {
            var child = subEl.childNodes[k];
            var whitespace = /^\s*$/;
            if (child.nodeName === "#text" && !whitespace.test(child.nodeValue)) {
              text = child.nodeValue;
              break;
            }
          }
        }
      }
      return text;
    };

    var getFromDiv = function getFromDiv(parent) {
      // Handles <div>
      return parent.textContent || parent.innerText;
    };

    // Change body to whatever element your main Markdown content lives.
    var body = document.querySelectorAll("body");
    var blocks = document.querySelectorAll("pre." + className + ",div." + className

      // Is there a settings object?
    ); var config = settings === void 0 ? {} : settings;

    // Find the UML source element and get the text
    for (var i = 0; i < blocks.length; i++) {
      var parentEl = blocks[i];
      var el = document.createElement("div");
      el.className = className;
      el.style.visibility = "hidden";
      el.style.position = "absolute";

      var text = parentEl.tagName.toLowerCase() === "pre" ? getFromCode(parentEl) : getFromDiv(parentEl);

      // Insert our new div at the end of our content to get general
      // typeset and page sizes as our parent might be `display:none`
      // keeping us from getting the right sizes for our SVG.
      // Our new div will be hidden via "visibility" and take no space
      // via `position: absolute`. When we are all done, use the
      // original node as a reference to insert our SVG back
      // into the proper place, and then make our SVG visible again.
      // Lastly, clean up the old node.
      body[0].appendChild(el);
      var diagram = converter.parse(text);
      diagram.drawSVG(el, config);
      el.style.visibility = "visible";
      el.style.position = "static";
      parentEl.parentNode.insertBefore(el, parentEl);
      parentEl.parentNode.removeChild(parentEl);
    }
  });

  (function () {
    var onReady = function onReady(fn) {
      if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", fn);
      } else {
        document.attachEvent("onreadystatechange", function () {
          if (document.readyState === "interactive") {
            fn();
          }
        });
      }
    };

    onReady(function () {
      if (typeof flowchart !== "undefined") {
        uml(flowchart, "uml-flowchart");
      }

      if (typeof Diagram !== "undefined") {
        uml(Diagram, "uml-sequence-diagram", { theme: "simple" });
      }
    });
  })();

}());